package club.zhangliyuanblog.controller;

import club.zhangliyuanblog.entity.Article;
import club.zhangliyuanblog.service.ArticleService;
import club.zhangliyuanblog.vo.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author liyuan.zhang
 * @date 2021/2/21 17:15
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*",maxAge = 3600)
@Api("文章api")
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping("/save")
    public Result save(@RequestBody Article article){
        Result result = new Result();
        log.info("文章对象是{}", article);
        article.setCreateTime(new Date());
        try {
            articleService.saveArticle(article);
            result.setMsg("添加文章成功");
        } catch (Exception e) {
            result.setStatus(false);
            result.setMsg("添加文章失败");
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/article/{id}")
    public Article getArticleById(@PathVariable(value = "id") Integer id){
        return articleService.getArticleById(id);
    }
}
